﻿Imports System.Data.OleDb
Imports BUS
Imports DTO
Public Class frmDangNhap
    Private nvBus As New NhanVienBUS()
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUserName.TextChanged

    End Sub
    Protected Function ConnectionData() As OleDbConnection
        Dim strCn As String
        strCn = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = QLKS.mdb"
        Dim cn As New OleDbConnection(strCn)
        cn.Open()
        Return cn
    End Function
    Public Function KiemTraDieuKienNhap() As String
        Dim kq As String = ""
        Dim m As Integer
        Dim hop_le As Boolean
        If txtUserName.Text.Trim = "" Then
            kq = kq & "Chưa nhập Username" & vbCrLf
            hop_le = Integer.TryParse(txtUserName.Text, m)
            
        End If
        If txtPassword.Text.Trim = "" Then
            kq = kq & "Chưa nhập Password " & vbCrLf
            hop_le = Integer.TryParse(txtPassword.Text, m)
            
        End If
        Return kq
    End Function
    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnDangNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDangNhap.Click
        ' Xu ly dang nhap
        ' Lay du lieu cua 2 o textbox
        ' viet cai ham kiem tra user.pass
        ' checkDangNhap(username, pass)
        ' if select * from table user where user = textbox user va` pass = textbox pass
        ' msgbox : dang nhap thanh cong
        ' roi
        ' lam di
        CheckDangNhap()
    End Sub
    Private Sub CheckDangNhap()
        'ByVal username As String, ByVal password As String
        Try
            Dim chon_loi As String = KiemTraDieuKienNhap()
            If chon_loi = "" Then
                Dim cn As OleDbConnection
                cn = Me.ConnectionData()
                Dim dt As New DataTable
                Dim username = Me.txtUserName.Text
                Dim password = Me.txtPassword.Text
                dt = nvBus.LayBang(username, password)
                Dim countr As Integer = dt.Rows.Count

                If countr = 1 Then
                    MessageBox.Show("Đăng nhập thành công", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Me.Hide()

                    Dim frmain As New frmMain
                    frmain.Show()

                End If
end if
        Catch ex As Exception
            MessageBox.Show("Đăng nhập thất bại", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information)
            MsgBox(ex.ToString)

        End Try

    End Sub
End Class